package model

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ UmsMemberModel = (*customUmsMemberModel)(nil)

type (
	// UmsMemberModel is an interface to be customized, add more methods here,
	// and implement the added methods in customUmsMemberModel.
	UmsMemberModel interface {
		umsMemberModel
		GetCountByPhone(ctx context.Context, phone string) (int, error)
		FindOneByPhone(ctx context.Context, phone string) (string, error)
	}

	customUmsMemberModel struct {
		*defaultUmsMemberModel
	}
)

func (c *customUmsMemberModel) FindOneByPhone(ctx context.Context, phone string) (string, error) {
	query := fmt.Sprintf("SELECT password FROM ums_member WHERE ums_member.mobile = ?")
	var pwd string
	err := c.conn.QueryRowCtx(ctx, &pwd, query, phone)
	if err != nil {
		return "", err
	}
	return pwd, nil
}

func (c *customUmsMemberModel) GetCountByPhone(ctx context.Context, phone string) (int, error) {
	query := fmt.Sprintf("SELECT COUNT(*) FROM ums_member WHERE ums_member.mobile = ?")
	var cnt int
	err := c.conn.QueryRowCtx(ctx, &cnt, query, phone)
	if err != nil {
		return 0, err
	}
	return cnt, nil
}

// NewUmsMemberModel returns a model for the database table.
func NewUmsMemberModel(conn sqlx.SqlConn) UmsMemberModel {
	return &customUmsMemberModel{
		defaultUmsMemberModel: newUmsMemberModel(conn),
	}
}
